package cn.xm.lingshi.service.impl;

import java.util.List;

import cn.xm.lingshi.dao.YongHuDao;
import cn.xm.lingshi.dao.impl.YongHuDaoImpl;
import cn.xm.lingshi.pojo.Address;
import cn.xm.lingshi.pojo.PageBean;
import cn.xm.lingshi.pojo.User;
import cn.xm.lingshi.pojo.YongHu;
import cn.xm.lingshi.service.YonghuService;

/**
 * 用户业务逻辑
 **/
public class YongHuServiceImpl implements YonghuService {

	private YongHuDao yh = new YongHuDaoImpl();

	@Override
	public YongHu dengLu(String zhangHao) {

		return yh.dengLu(zhangHao);
	}

	@Override
	public int chaXunZhuCe(String zhangHao) {

		return yh.chaXunZhuCe(zhangHao);
	}

	@Override
	public int tianJiao(YongHu yongHu) {

		return yh.tianJiao(yongHu);
	}

	@Override
	public PageBean<User> queryUserByPageNo(int pageNo, int pageSize,
			String queryName, int roleId) {
		PageBean<User> pageBean = new PageBean<User>();
		try {
			// 设置当前页码
			pageBean.setCurrPageNo(pageNo);
			// 设置每页的记录数
			pageBean.setPageSize(pageSize);
			pageBean.setTotalCount(this.queryTotalRecord(queryName, roleId));
			// 根据页码和每页记录数获取偏移量
			int offSet = (pageNo - 1) * pageSize;
			List<User> list = yh.queryUserByOffset(offSet, pageSize,
					queryName, roleId);
			// 设置每页的数据
			pageBean.setList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageBean;
	}

	@Override
	public int queryTotalRecord(String queryName, int roleId) {
		try {
			return yh.queryTotalRecord(queryName, roleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int queryTotalPageCount(int pageSize, String queryName, int roleId) {
		PageBean<User> pageBean = new PageBean<User>();
		// 总记录数
		int totalCount = this.queryTotalRecord(queryName, roleId);
		// 设置每页的记录数
		pageBean.setPageSize(pageSize);
		// 计算总页数
		pageBean.setTotalCount(totalCount);
		return pageBean.getTotalPageCount();
	}

	@Override
	public boolean addAddress(Address a) {
		int i=0;
		try {
			 i=yh.addAddress(a);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(i==1){
			return true;
		}
		return false;
	}

	@Override
	public int modpassword(int userid, String password){
		
		try {
			return yh.modpassword(userid, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<Address> addAddress(int userid) {
		
		try {
			return yh.addAddress(userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
